<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\set;
use App\Http\model\Curdpost;
class SetController extends BaseController
{	
	/**
	 * [setStatus 修改司机状态]
	 * @Author   孙丽男
	 * @DateTime 2018-06-11
	 * @param    [type]
	 * @version  6.2
	 */
    public function setLines(Request $request){
    	$result = array();
        $id = $request->get('id');
    	if ($id && $request['is_zx'] && $request['Longitude'] && $request['Latitude']) {
    		$driver = new set;
    		$res = $driver->setLinesModel($id,$request['is_zx']);
    
    		if ($res['code'] == 1003) {
                $result['code'] = 1003;
                $result['msg'] = '还有订单未结束';
            }elseif ($res['code'] == 1004) {
                $result['code'] = 1004;
                $result['msg'] = '失败';
            }else{
                //数据同步
                $curd = new Curdpost;
                $d = $res['data'];
                $syncData['CompanyId'] = '公司标识';
                $syncData['LicenseId'] = $d['licenseId'];
                $syncData['VehicleNo'] = $d['num'];
                $syncData['Longitude'] = $request['Longitude'];
                $syncData['Latitude'] = $request['Latitude'];
                $syncData['Encrypt'] = 3;
                $where = "VehicleNo=".'"'.$d['num'].'"';
                if ($request['is_zx'] == 1) {
                    $syncData['LoginTime'] = date('ymdhis',time());
                    /**
                     * 查询是否有数据
                     */
                    $findDriver = $curd->selectArr('operatelogin',$where);

                    if ($findDriver['code'] == 1000) {
                        
                        $resDriver = $curd->NewUpdate('operatelogin',$syncData,$where);

                    }else{

                        $resDriver = $curd->NewInsert('operatelogin',$syncData);
                    }
                }else{
                    $syncData['LogoutTime'] = date('ymdhis',time());
                    /**
                     * 查询是否有数据
                     */
                    $findDriver = $curd->selectArr('operatelogout',$where);

                    if ($findDriver['code'] == 1000) {

                        $resDriver = $curd->NewUpdate('operatelogout',$syncData,$where);

                    }else{

                        $resDriver = $curd->NewInsert('operatelogout',$syncData);
                    }
                }

                if ($resDriver) {
                    $result['code'] = 1000;
                    $result['msg'] = '修改成功';
                }else{
                    $result['code'] = 1010;
                    $result['msg'] = '同步失败';
                }
            }
            return json_encode($result);
    	}else{
    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);
    	}
    }
}
